#include "authorization.h"
#include "./ui_authorization.h"

#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlDriver>
#include <QMessageBox>

Authorization::Authorization(QWidget *parent) :
  QWidget(parent),
  ui(new Ui::Authorization)
{
  ui->setupUi(this); 
}

Authorization::~Authorization()
{
  delete ui;
}

void Authorization::on_pushButton_clicked()
{
  QSqlQuery query;
  query.exec("SELECT \"User\".\"Login\", \"User\".\"Password\", \"User\".\"Name\", \"User\".\"Surname\", \"User\".\"Patronymic\", \"User\".\"Phone_number\", \"Role\".\"Value\", \"User\".\"image\"  FROM \"User\", \"Role\" WHERE \"User\".\"role_id\" = \"Role\".\"Id\"");

  bool isAuthentication;
  while(query.next())
  {

    if(ui->LoginBox->text() == query.value(0).toString() && ui->PasswordBox->text() == query.value(1).toString())
    {
        Singleton* s1 = Singleton::getInstance();
        s1->name = query.value(2).toString();
        s1->surname = query.value(3).toString();
        s1->patronymic = query.value(4).toString();
        s1->role = query.value(6).toString();
        s1->image = query.value(7).toByteArray();
        isAuthentication = true; 
        break;
    }
    else
        isAuthentication = false;
  }

  if(isAuthentication)
  {
      mainWindow = new MainWindow;
      mainWindow->show();
      this->close();
  }
  else
      QMessageBox::warning(0, "Внимание", "Невераный логин или пароль");
}

void Authorization::on_checkBox_clicked(bool checked)
{
    if(checked)
        ui->PasswordBox->setEchoMode(QLineEdit::Normal);
    else
        ui->PasswordBox->setEchoMode(QLineEdit::Password);

}


void Authorization::on_checkBox_stateChanged(int arg1)
{

}

